#include<vector>
#include<algorithm>
#include<iostream>
using std::vector;



class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> res;
        if (nums.size() < 3) return res;
        // 先进行排序
        std::sort(nums.begin(), nums.end());
        
        
        // 双指针
        for (int k = 0; k < int(nums.size())-2 && nums[k] <= 0; k++) {
            //如果nums[k]重复，跳过
            if (k > 0 && nums[k] == nums[k-1]) continue;
            int i = k + 1, j = int(nums.size()) - 1;
            while (i < j && i != -1 & j != -1) {
                auto sum = nums[k] + nums[i] + nums[j];
                if (sum == 0) {
                    res.push_back({nums[k], nums[i], nums[j]});
                    auto left = nums[i], right = nums[j];
                    while (i < j && nums[i] == left) i++;
                    while (j > i && nums[j] == right) j--;

                }
                else if (sum < 0) {
                    i = this->find1sGreater(nums, i, j, -nums[k]-nums[j]);
                    //i++;
                }
                else {
                    j = this->find1stLess(nums, i, j, -nums[k]-nums[i]);
                    //j--;
                }

            }
        }
        return res;

    };

    int find1stLess(vector<int>& nums, int left, int right, int value){
        // 找到第一个小等于value值的索引，找不到返回-1
        if (right - left < 2) return -1;
        auto mid = (right + left) / 2;
        if (nums[mid] == value) return mid;
        else if (nums[mid] < value) {
            if (nums[mid+1] > value) return mid;
            else return this->find1stLess(nums, mid, right, value);
        }
        else {
            return this->find1stLess(nums, left, mid, value);
        }
    }

    int find1sGreater(vector<int>& nums, int left, int right, int value) {
        // 找到第一个大等于value值的索引
        if (right - left < 2) return -1;
        auto mid = (right + left) / 2;
        if (nums[mid] == value) return mid;
        else if (nums[mid] < value) {
            return this->find1sGreater(nums, mid, right, value);
        }
        else {
            if (nums[mid-1] < value) return mid;
            else return this->find1sGreater(nums, left, mid, value);
        }
    }
};

int main() {
    vector<int> input = {};
    int n;
    while (std::cin >> n) {
        input.push_back(n);
    }
    std::cout << "input: \n";
    for (auto k: input) std::cout << k << " ";
    std::cout << "\n";
    

    auto res = Solution().threeSum(input);
    std::cout << "triple tuple size: " << res.size() << "\n";
    for (auto v : res) std::cout << "[" << v[0] << "," << v[1] << "," << v[2] << "]\n";
    return 0;
}